package com.hongdee.market.bc.service.impl;

import com.hongdee.common.dao.IHibernateDao;

import com.hongdee.market.bc.dao.ITBcCustomLevelDao;
import com.hongdee.market.bc.entity.TBcCustomeItem;
import com.hongdee.market.bc.service.ITBcCustomItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dell on 2017/5/15.
 * * 客户评分项目管理
 */
@Service
public class TBcCustomItemServiceImpl implements ITBcCustomItemService {
    @Autowired
    private ITBcCustomLevelDao itBcCustomLevelDao;
    private String tabName = "T_BC_CUSTOME_ITEM";
    @Autowired
    private IHibernateDao iHibernateDao;

    /*
    * 停用启用
    * */
    @Override
    public void update(String id, String status) throws Exception {
        String hql = "update TBcCustomeItem set status = :status where id = :id ";
        Map params = new HashMap();
        params.put("id", id);
        params.put("status", status);
        this.itBcCustomLevelDao.update(hql, params);
    }

    public TBcCustomeItem saveOrUpdate(TBcCustomeItem tBcCustomeItem) throws Exception {
        //根据项目编码、项目名称去重
        if (null == tBcCustomeItem.getId() || StringUtils.isEmpty(tBcCustomeItem.getId())) {
            //新增时判断是否已经存在
            if (isExist(tBcCustomeItem.getItemCode(), tBcCustomeItem.getItemName(), 0, null)) {
                throw new Exception("项目名称或项目编码已存在");
            }
            tBcCustomeItem.setStatus("1");
            this.itBcCustomLevelDao.saveOrUpdate(tBcCustomeItem);
        } else {
            if (isExist(tBcCustomeItem.getItemCode(), tBcCustomeItem.getItemName(), 1, tBcCustomeItem.getId())) {
                throw new Exception("项目名称或项目编码已存在");
            }

            tBcCustomeItem.setStatus("1");
            this.itBcCustomLevelDao.saveOrUpdate(tBcCustomeItem);
        }
        return tBcCustomeItem;
    }

    //判断项目编码，项目名称是否重复
    private boolean isExist(String itemCode, String itemName, int type, String id) throws Exception {
        String sql = "select * from " + tabName + " where ( item_code= ? or item_name = ? ) ";
        List param = new ArrayList();
        param.add(itemCode);
        param.add(itemName);
        //项目编码和项目名称不可以重复
        if (1 == type) {
            sql += " and  id != ? ";
            param.add(id);
        }
        Map map = this.itBcCustomLevelDao.page(sql, param.toArray(), null, null);
        return Integer.valueOf(map.get("total").toString()) > 0;
    }

    /*
    * 根据条件查询
    * */
    @Override
    public Map info(String id) throws Exception {
        String sql = "select * from " + tabName + " where id = ? ";
        return this.itBcCustomLevelDao.info(sql, id);
    }

    /*
    * 分页
    * **/
    @Override
    public Map newPage(String start, String limit, String searchValue) throws Exception {
        String sql = "select * from  T_BC_CUSTOME_ITEM ";
        List<Object> params = new ArrayList();
        if (!StringUtils.isEmpty(searchValue)) {
            sql += " where item_name like ? or item_code like ? ";
            params.add("%" + searchValue + "%");
            params.add("%" + searchValue + "%");
        }
        //根据排序字段升序排序
        sql += "order by disp_order asc";
        return this.itBcCustomLevelDao.page(sql, params.toArray(), start, limit);
    }

    /***
     * 评分项目下拉
     * @return
     * @throws Exception
     */
    @Override
    public List<Map> combo() throws Exception {
        String sql = " select * from  T_BC_CUSTOME_ITEM  order by disp_order asc ";
        List<Map<String, Object>> mapList = this.itBcCustomLevelDao.list(sql);
        List<Map> resList = new ArrayList<Map>();
        Map temp;
        for (Map map : mapList) {
            temp = new HashMap();
            temp.put("value", map.get("itemCode"));
            temp.put("label", map.get("itemName"));
            resList.add(temp);

        }
        return resList;
    }

    /***
     * 评分项目转换
     * @return
     * @throws Exception
     */
    @Override
    public Map convert() throws Exception {
        String sql = " select * from  T_BC_CUSTOME_ITEM  order by disp_order asc ";
        List<Map<String, Object>> mapList = this.itBcCustomLevelDao.list(sql);
        Map temp = new HashMap();
        for (Map map : mapList) {
            temp.put(map.get("itemCode"), map.get("itemName"));
        }
        return temp;
    }
}
